package org.jeecgframework.web.service.impl;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.ArrayUtils;
import org.apache.log4j.Logger;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.extend.swftools.SwfToolsUtil;
import org.jeecgframework.core.util.*;
import org.jeecgframework.web.service.CbsQuotationServiceI;
import org.jeecgframework.web.service.ZyEquipmentSpareServiceI;
import org.jeecgframework.web.system.pojo.base.*;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.web.util.NumberToLoCapital;
import org.jeecgframework.web.util.StaticValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("cbsQuotationService")
@Transactional
public class CbsQuotationServiceImpl extends CommonServiceImpl implements CbsQuotationServiceI {

    private static final Logger logger = Logger.getLogger(CbsQuotationServiceImpl.class);

    @Autowired
    private ZyEquipmentSpareServiceI zyEquipmentSpareService;
    @Autowired
    private SystemService systemService;

    public <T> void delete(T entity) {
        super.delete(entity);
        //执行删除操作配置的sql增强
        this.doDelSql((CbsQuotationEntity) entity);
    }

    public <T> Serializable save(T entity) {
        Serializable t = super.save(entity);
        //执行新增操作配置的sql增强
        this.doAddSql((CbsQuotationEntity) entity);
        return t;
    }

    public <T> void saveOrUpdate(T entity) {
        super.saveOrUpdate(entity);
        //执行更新操作配置的sql增强
        this.doUpdateSql((CbsQuotationEntity) entity);
    }

    /**
     * 默认按钮-sql增强-新增操作
     *
     * @return
     */
    public boolean doAddSql(CbsQuotationEntity t) {
        return true;
    }

    /**
     * 默认按钮-sql增强-更新操作
     *
     * @return
     */
    public boolean doUpdateSql(CbsQuotationEntity t) {
        return true;
    }

    /**
     * 默认按钮-sql增强-删除操作
     *
     * @return
     */
    public boolean doDelSql(CbsQuotationEntity t) {
        return true;
    }

    /**
     * 替换sql中的变量
     *
     * @param sql
     * @return
     */
    public String replaceVal(String sql, CbsQuotationEntity t) {
        String tempSql = sql;
        tempSql = tempSql.replace("#{id}", String.valueOf(t.getId()))
                .replace("#{quotation_num}", String.valueOf(t.getQuotationNum()))
                .replace("#{total}", String.valueOf(t.getTotal()))
                .replace("#{pdf_url}", String.valueOf(t.getPdfUrl()))
                .replace("#{create_name}", String.valueOf(t.getCreateName()))
                .replace("#{create_date}", String.valueOf(t.getCreateDate()))
                .replace("#{create_by}", String.valueOf(t.getCreateBy()))
                .replace("#{update_name}", String.valueOf(t.getUpdateName()))
                .replace("#{update_by}", String.valueOf(t.getUpdateBy()))
                .replace("#{update_date}", String.valueOf(t.getUpdateDate()))
                .replace("#{UUID}", UUID.randomUUID().toString());
        return tempSql;
    }

    public String genQuotationNumber(CbsProcurementNoteEntity note) {
        String time = DataUtils.formatDate("yyMMdd");
        List<CbsQuotationEntity> quotationList = this.findHql("from CbsQuotationEntity where date(createDate) = curdate() ");
        String num;
        String type = "";
        String quotationNumber;
        num = Integer.toString(quotationList.size()+ 1)  + "";

        if(quotationList.isEmpty()){
            num = "01";
        }else if(quotationList.size() < 10){
            num = "0" + num;
        }
        try {
            List<CbsProcurementPartEsEntity> paertEsList = note.getPaertEsList();
            ZyEquipmentSpareEntity spare = null;
            for (CbsProcurementPartEsEntity paertEs : paertEsList) {
                spare = paertEs.getEquipmentSpare();
                if (spare == null) {
                    continue;
                }
                //在保
                if (StringUtil.isNotEmpty(spare.getRepairEndDate()) && spare.getRepairEndDate().after(new Date())) {
                    type = "C";
                    //保外
                } else {
                    //有上门服务费
                    if ("1".equals(note.getIsOnsiteServices())) {
                        type = "S";
                    } else {
                        //客户
                        String checkBp = zyEquipmentSpareService.checkBP(paertEs.getEquipmentSpare().getBpNo());
                        if ("0".equals(note.getClientType())) {

                            if ("z".equals(checkBp)) {
                                type = "A";
                            } else if ("h".equals(checkBp)) {
                                type = "B";
                            }
                        } else {//第三方
                            if ("z".equals(checkBp) || "h".equals(checkBp)) {
                                type = "E";
                            } else {
                                type = "T";
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            logger.error("genQuotationNumber:"+e.getMessage(),e);
        }
        quotationNumber = "Q-" + time + num + type;
        return quotationNumber;
    }


    public List<CbsQuotationDetailEntity> genQuotationDetail(CbsProcurementNoteEntity note, CbsQuotationEntity quotation) {
        DecimalFormat df = new DecimalFormat("#.00");
        Double disCount = Double.parseDouble(df.format(quotation.getDisCount() / 100));
        List<CbsQuotationDetailEntity> detailList = new ArrayList<>();
        Double countMoney = 0.00;
        for (CbsProcurementPartEntity part : note.getPartList()) {
            CbsQuotationDetailEntity detail = new CbsQuotationDetailEntity();
            Double money = part.getPrice();
            //原价
            detail.setUnitPrice((int) Math.rint(money));
            //优惠价
            detail.setDisCountPrice((int) Math.rint(money * disCount));
            //最终价
            detail.setLastPrice(detail.getDisCountPrice());
            //小计
            detail.setTotal((int) Math.rint(detail.getDisCountPrice() * Integer.parseInt(part.getNum())));
            //数量
            detail.setNum(Integer.parseInt(part.getNum()));
            countMoney = countMoney + detail.getTotal();
            detail.setProcurementPart(part);
            detail.setQuotation(quotation);
            detailList.add(detail);
        }
        //需要上门服务  计算上门服务费
        if ("1".equals(note.getIsOnsiteServices())) {
            quotation.setTravelNumber(1);
            quotation.setManpowerNumber(1);
            quotation.setManpowerPrice(2500);
            //0.是省会 1.不是
            if ("0".equals(note.getCinema().getProvince().getIsCapital())) {
                quotation.setTravelPrice(1000);
            } else {
                quotation.setTravelPrice(2000);
            }
        }
        quotation.setTotal((int) Math.rint(countMoney) + "");
        this.saveOrUpdate(quotation);
        return detailList;
    }

    public XSSFCellStyle setCellStyle(XSSFWorkbook xssf) {
        XSSFCellStyle cellStyle = xssf.createCellStyle();
        cellStyle.setAlignment(CellStyle.ALIGN_CENTER);//水平居中
        cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);//垂直居中
        cellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN); //下边框
        cellStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);//左边框
        cellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);//上边框
        cellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);//右边框
        XSSFFont font = xssf.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) 12);//设置字体大小
        cellStyle.setFont(font);
        return cellStyle;
    }

    public String genQuotationExcel(CbsQuotationEntity quotation, HttpServletRequest request) {
        String outPath = StaticValue.UPLOAD_PATH + UUIDGenerator.generate().replace("-", "") + ".xlsx";
        DecimalFormat df = new DecimalFormat("#,###");
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yy", Locale.ENGLISH);
        OutputStream os = null;
        try {
            Integer maxSize = quotation.getNote().getPaertEsList().size() > quotation.getQuotationDetailList().size() ? quotation.getNote().getPaertEsList().size() : quotation.getQuotationDetailList().size();
            //第三方
            if ("1".equals(quotation.getNote().getClientType())) {
                os = new FileOutputStream(outPath);
                InputStream is = new FileInputStream(request.getRealPath("/") + "export/template/ThirdPartyTemplate.xlsx");
                XSSFWorkbook xssf = new XSSFWorkbook(is);
                XSSFSheet sheet = xssf.getSheetAt(0);
                XSSFCellStyle cellStyle = setCellStyle(xssf);
                sheet.getRow(1).getCell(12).setCellValue(quotation.getQuotationNum());
                sheet.getRow(4).getCell(12).setCellValue(sdf.format(quotation.getCreateDate()));
                sheet.getRow(7).getCell(1).setCellValue(quotation.getNote().getProcurement().getDepartname());
                sheet.getRow(7).getCell(6).setCellValue(quotation.getNote().getContactName());
                sheet.getRow(8).getCell(6).setCellValue(quotation.getNote().getContactPhone());
                sheet.getRow(8).getCell(7).setCellValue(quotation.getNote().getContactMail());
                sheet.getRow(9).getCell(0).setCellValue(quotation.getNote().getWarranty());
                sheet.getRow(11).getCell(0).setCellValue(quotation.getNote().getComment());
                sheet.getRow(21).getCell(13).setCellValue("￥" + df.format(Integer.parseInt(quotation.getTotal())));
                String locapital = NumberToLoCapital.number2CNMontrayUnit(new BigDecimal(quotation.getTotal()));
                sheet.getRow(21).getCell(11).setCellValue(locapital);
                for (int i = 0; i < quotation.getNote().getPaertEsList().size(); i++) {
                    XSSFRow row = sheet.createRow(36 + i);
                    if (i == 0) {
                        row.createCell(0).setCellValue(quotation.getNote().getCinema().getName());
                        row.getCell(0).setCellStyle(cellStyle);
                    } else {
                        row.createCell(0).setCellStyle(cellStyle);
                    }
                    row.createCell(1).setCellValue(quotation.getNote().getPaertEsList().get(i).getEquipmentSpare().getType().getTypeDesc());
                    row.createCell(2).setCellValue(quotation.getNote().getPaertEsList().get(i).getEquipmentSpare().getSn());
                    row.getCell(1).setCellStyle(cellStyle);
                    row.getCell(2).setCellStyle(cellStyle);
                    if (quotation.getQuotationDetailList().size() < i + 1) {
                        sheet.addMergedRegion(new CellRangeAddress(row.getRowNum(), row.getRowNum(), 4, 7));
                        sheet.addMergedRegion(new CellRangeAddress(row.getRowNum(), row.getRowNum(), 11, 12));
                        sheet.addMergedRegion(new CellRangeAddress(row.getRowNum(), row.getRowNum(), 13, 14));
                        for (int j = 3; j < 15; j++) {
                            row.createCell(j).setCellStyle(cellStyle);
                        }
                    }
                }
                for (int i = 0; i < quotation.getQuotationDetailList().size(); i++) {
                    XSSFRow row;
                    if (quotation.getNote().getPaertEsList().size() >= i + 1) {
                        row = sheet.getRow(36 + i);
                    } else {
                        row = sheet.createRow(36 + i);
                        row.createCell(0).setCellStyle(cellStyle);
                        row.createCell(1).setCellStyle(cellStyle);
                        row.createCell(2).setCellStyle(cellStyle);
                    }

                    row.createCell(3).setCellValue(quotation.getQuotationDetailList().get(i).getProcurementPart().getPartInfo().getPartNo());
                    sheet.addMergedRegion(new CellRangeAddress(36 + i, 36 + i, 4, 7));
                    sheet.addMergedRegion(new CellRangeAddress(36 + i, 36 + i, 11, 12));
                    sheet.addMergedRegion(new CellRangeAddress(36 + i, 36 + i, 13, 14));
                    row.createCell(4).setCellValue(quotation.getQuotationDetailList().get(i).getProcurementPart().getPartInfo().getChineseDes());
                    row.createCell(8).setCellValue(quotation.getQuotationDetailList().get(i).getNum());
                    row.createCell(9).setCellValue(quotation.getQuotationDetailList().get(i).getProcurementPart().getPartInfo().getMarketUnit());
                    row.createCell(10).setCellValue("￥" + df.format(quotation.getQuotationDetailList().get(i).getUnitPrice()));
                    row.createCell(11).setCellValue("￥" + df.format(quotation.getQuotationDetailList().get(i).getLastPrice()));
                    row.createCell(13).setCellValue("￥" + df.format(quotation.getQuotationDetailList().get(i).getTotal()));
                    row.getCell(3).setCellStyle(cellStyle);
                    row.getCell(4).setCellStyle(cellStyle);
                    row.createCell(5).setCellStyle(cellStyle);
                    row.createCell(6).setCellStyle(cellStyle);
                    row.createCell(7).setCellStyle(cellStyle);
                    row.getCell(8).setCellStyle(cellStyle);
                    row.getCell(9).setCellStyle(cellStyle);
                    row.getCell(10).setCellStyle(cellStyle);
                    row.getCell(11).setCellStyle(cellStyle);
                    row.createCell(12).setCellStyle(cellStyle);
                    row.getCell(13).setCellStyle(cellStyle);
                    row.createCell(14).setCellStyle(cellStyle);
                }
                sheet.shiftRows(14, sheet.getLastRowNum(), maxSize, false, true);
                int start = 36 + maxSize;
                int end = 36 + maxSize + maxSize;
                int dowRow = -22 - maxSize;
                sheet.shiftRows(start, end, dowRow, false, true);
                for (int i = 0; i < maxSize; i++) {
                    XSSFRow row = sheet.getRow(14 + i);
                    row.setHeightInPoints(30);
                }
                XSSFRow row = sheet.getRow(14 + maxSize);
                for (int i = 0; i < 15; i++) {
                    row.createCell(i).setCellStyle(cellStyle);
                }
                float height = ((int) quotation.getNote().getWarranty().length() / 55) * 30;
                sheet.getRow(16).setHeightInPoints(height < 30 ? 30 : height);
                height = ((int) quotation.getNote().getComment().length() / 55) * 30;
                sheet.getRow(18).setHeightInPoints(height < 30 ? 30 : height);
                xssf.write(os);


                //客户
            } else {
                os = new FileOutputStream(outPath);
                InputStream is = new FileInputStream(request.getRealPath("/") + "export/template/CustomerTemplate.xlsx");
                XSSFWorkbook xssf = new XSSFWorkbook(is);
                XSSFSheet sheet = xssf.getSheetAt(0);
                XSSFCellStyle cellStyle = setCellStyle(xssf);
                sheet.getRow(1).getCell(8).setCellValue(quotation.getQuotationNum());
                sheet.getRow(4).getCell(8).setCellValue(sdf.format(quotation.getCreateDate()));
                sheet.getRow(8).getCell(0).setCellValue(quotation.getNote().getCinema().getName());
                sheet.getRow(8).getCell(0).setCellValue(quotation.getNote().getCinema().getName());
                sheet.getRow(8).getCell(6).setCellValue(quotation.getNote().getContactPhone());
                sheet.getRow(10).getCell(0).setCellValue(quotation.getNote().getCinema().getAddress());
                sheet.getRow(10).getCell(6).setCellValue(quotation.getNote().getContactFax());
                sheet.getRow(12).getCell(0).setCellValue(quotation.getNote().getContactName());
                sheet.getRow(12).getCell(6).setCellValue(quotation.getNote().getContactMail());
                sheet.getRow(32).getCell(9).setCellValue("￥" + df.format(Integer.parseInt(quotation.getTotal())));
                String locapital = NumberToLoCapital.number2CNMontrayUnit(new BigDecimal(quotation.getTotal()));
                sheet.getRow(32).getCell(7).setCellValue(locapital);

                StringBuilder snsBuilder = new StringBuilder();
                for (CbsProcurementPartEsEntity es : quotation.getNote().getPaertEsList()) {
                    snsBuilder.append(es.getEquipmentSpare().getSn() + "丶");
                }
                String sns = snsBuilder.toString();
                sheet.addMergedRegion(new CellRangeAddress(14, 14, 0, 5));
                sns = StringUtil.isNotEmpty(sns) ? sns.substring(0, sns.length() - 1) : "";
                sheet.getRow(14).getCell(0).setCellValue(sns);

                sheet.getRow(14).getCell(6).setCellValue(quotation.getNote().getServicePlace());
                sheet.getRow(16).getCell(0).setCellValue(quotation.getNote().getWarranty());
                sheet.getRow(18).getCell(0).setCellValue(quotation.getNote().getServicesPremise());
                for (int i = 0; i < quotation.getQuotationDetailList().size(); i++) {
                    XSSFRow row = sheet.createRow(50 + i);
                    for (int j = 0; j < 11; j++) {
                        row.createCell(j).setCellStyle(cellStyle);
                    }
                    sheet.addMergedRegion(new CellRangeAddress(50 + i, 50 + i, 1, 4));
                    sheet.addMergedRegion(new CellRangeAddress(50 + i, 50 + i, 7, 8));
                    sheet.addMergedRegion(new CellRangeAddress(50 + i, 50 + i, 9, 10));
                    row.getCell(0).setCellValue(quotation.getQuotationDetailList().get(i).getProcurementPart().getPartInfo().getPartNo());
                    row.getCell(1).setCellValue(quotation.getQuotationDetailList().get(i).getProcurementPart().getPartInfo().getChineseDes());
                    row.getCell(5).setCellValue(quotation.getQuotationDetailList().get(i).getNum());
                    row.getCell(6).setCellValue(quotation.getQuotationDetailList().get(i).getProcurementPart().getPartInfo().getMarketUnit());
                    row.getCell(7).setCellValue("￥" + df.format(quotation.getQuotationDetailList().get(i).getUnitPrice()));
                    row.getCell(9).setCellValue("￥" + df.format(quotation.getQuotationDetailList().get(i).getTotal()));
                }
                int flag = 0;
                //需要上门服务
                if ("1".equals(quotation.getNote().getIsOnsiteServices())) {
                    flag = 2;
                    XSSFRow row = sheet.createRow(50 + quotation.getQuotationDetailList().size());
                    for (int j = 0; j < 11; j++) {
                        row.createCell(j).setCellStyle(cellStyle);
                    }
                    sheet.addMergedRegion(new CellRangeAddress(row.getRowNum(), row.getRowNum(), 1, 4));
                    sheet.addMergedRegion(new CellRangeAddress(row.getRowNum(), row.getRowNum(), 7, 8));
                    sheet.addMergedRegion(new CellRangeAddress(row.getRowNum(), row.getRowNum(), 9, 10));
                    row.getCell(0).setCellValue("上门服务费");
                    row.getCell(1).setCellValue("人工费");
                    row.getCell(5).setCellValue(StringUtil.isNotEmpty(quotation.getManpowerNumber())?quotation.getManpowerNumber():0);
                    row.getCell(6).setCellValue("EA");
                    row.getCell(7).setCellValue("￥" + df.format(StringUtil.isNotEmpty(quotation.getManpowerPrice())?quotation.getManpowerPrice():0));
                    if(StringUtil.isNotEmpty(quotation.getManpowerNumber())&&StringUtil.isNotEmpty(quotation.getManpowerNumber())){
                        row.getCell(9).setCellValue("￥" + df.format(quotation.getManpowerNumber() * quotation.getManpowerPrice()));
                    }else{
                        row.getCell(9).setCellValue("￥"+df.format(0));
                    }
                    XSSFRow rowt = sheet.createRow(51 + quotation.getQuotationDetailList().size());
                    for (int j = 0; j < 11; j++) {
                        rowt.createCell(j).setCellStyle(cellStyle);
                    }
                    sheet.addMergedRegion(new CellRangeAddress(rowt.getRowNum(), rowt.getRowNum(), 1, 4));
                    sheet.addMergedRegion(new CellRangeAddress(rowt.getRowNum(), rowt.getRowNum(), 7, 8));
                    sheet.addMergedRegion(new CellRangeAddress(rowt.getRowNum(), rowt.getRowNum(), 9, 10));
                    rowt.getCell(0).setCellValue("");
                    rowt.getCell(1).setCellValue("差旅费");
                    if(StringUtil.isNotEmpty(quotation.getTravelNumber())){
                        rowt.getCell(5).setCellValue(quotation.getTravelNumber());
                    }else{
                        rowt.getCell(5).setCellValue(0);
                    }
                    rowt.getCell(6).setCellValue("EA");
                    if(StringUtil.isNotEmpty(quotation.getTravelPrice())){
                        rowt.getCell(7).setCellValue("￥" + df.format(quotation.getTravelPrice()));
                    }else{
                        rowt.getCell(7).setCellValue("￥0");
                    }

                    if(StringUtil.isNotEmpty(quotation.getTravelNumber())&&StringUtil.isNotEmpty(quotation.getTravelPrice())){
                        rowt.getCell(9).setCellValue("￥" + df.format(StringUtil.isNotEmpty(quotation.getTravelNumber() * quotation.getTravelPrice())?quotation.getTravelNumber() * quotation.getTravelPrice():0));
                    }else {
                        rowt.getCell(9).setCellValue("￥0");
                    }

                }
                sheet.shiftRows(21, sheet.getLastRowNum(), quotation.getQuotationDetailList().size() + flag, false, true);
                int start = 50 + quotation.getQuotationDetailList().size() + flag;
                int end = 50 + quotation.getQuotationDetailList().size() * 2 + flag * 2;
                int dowRow = -29 - quotation.getQuotationDetailList().size() - flag;
                sheet.shiftRows(start, end, dowRow, false, true);
                float height = ((int) sns.split("丶").length / 5) * 30;
                sheet.getRow(14).setHeightInPoints(height < 30 ? 30 : height);

                if (StringUtil.isNotEmpty(quotation.getNote().getWarranty())) {
                    height = ((int) quotation.getNote().getWarranty().length() / 50) * 30;
                    sheet.getRow(16).setHeightInPoints(height < 30 ? 30 : height);
                }
                if (StringUtil.isNotEmpty(quotation.getNote().getServicesPremise())) {
                    height = ((int) quotation.getNote().getServicesPremise().length() / 50) * 30;
                    sheet.getRow(18).setHeightInPoints(height < 30 ? 30 : height);
                }
                XSSFRow row = sheet.getRow(21 + quotation.getQuotationDetailList().size() + flag);
                for (int i = 0; i < 11; i++) {
                    row.createCell(i).setCellStyle(cellStyle);
                }
                xssf.write(os);
            }


        } catch (Exception e) {
            logger.error("genQuotationExcel:"+e.getMessage(),e);
        } finally {
            try {
                os.close();
                SwfToolsUtil.convert2SWF(outPath);
            } catch (IOException e) {
                logger.error(e);
            }

        }
        return outPath;
    }

    public String getEsDepart(ZyEquipmentSpareEntity es) {
        //中影
        if (StaticValue.ORG_CODE_ZY.equals(es.getBpNo())) {
            return "z";
            //横店
        } else if (StaticValue.ORG_CODE_HD.equals(es.getBpNo())) {
            return "h";
        }
        //其他
        return "n";
    }

    public boolean canReportQuotationByHd(CbsProcurementNoteEntity note) {
        if(note == null || !"0".equals(note.getClientType()) || null == note.getPaertEsList()){
            return true;
        }
        List<CbsProcurementPartEsEntity> pareList = note.getPaertEsList();
        for (CbsProcurementPartEsEntity es : pareList) {
            if (es.getEquipmentSpare() != null
                    && oConvertUtils.isNotEmpty(es.getEquipmentSpare().getBpNo())
                    && "zh".contains(zyEquipmentSpareService.checkBP(es.getEquipmentSpare().getBpNo()))) {
                return false;
            }
        }
        return true;
    }

    @Override
    public CbsQuotationEntity createQuotation(CbsProcurementNoteEntity note, String type) {
        String quotationNumber = genQuotationNumber(note);
        CbsQuotationEntity quotation = new CbsQuotationEntity();
        quotation.setNote(note);
        quotation.setQuotationNum(quotationNumber);
        quotation.setDisCount(100);
        quotation.setType(type);
        quotation.setState("1");
        systemService.save(quotation);
        List<CbsQuotationDetailEntity> detailList = genQuotationDetail(note, quotation);
        systemService.batchSave(detailList);
        return quotation;
    }
}